#include <iostream>
#include <string>
using namespace std;

/*功能： 
1，添加密码
2，显示所有密码
3，删除密码
4，查找密码
5，修改密码
6，清除密码
0，退出密码箱     */

void showmenu()
{
    cout<<"welcome to password box"<<endl<<endl;
    cout<<"1,enter a new password:"<<endl<<endl;
    cout<<"2,display all the password:"<<endl<<endl;
    cout<<"3,delete an old password:"<<endl<<endl;
    cout<<"4,find an old  password:"<<endl<<endl;
    cout<<"5,correct an old password:"<<endl<<endl;
    cout<<"6,delete all the password:"<<endl<<endl;
    cout<<"0,get out of the password box"<<endl<<endl;
}

struct password
{
        string platform;
        string accout;
        string passcode;
};

struct passwordbox 
{
    int p_size;
    struct password passwordArray[1000];

};

void addpassword(struct passwordbox * a)
{
    if(a->p_size==1000)
    {
        cout<<"there are too many groups of passwords in the case."<<endl;
        cout<<"please delete some of them first."<<endl;
    }
    else
    {   
        a->p_size=a->p_size+1;

        string platfm;
        cout<<"please enter the platform  :"<<endl;
        cin>>platfm;
        a->passwordArray[a->p_size].platform=platfm;
        cout<<endl;

        string accot;
        cout<<"account:   "<<endl;
        cin>>accot;
        a->passwordArray[a->p_size].accout=accot;
        cout<<endl;

        string passwod;
        cout<<"password:   "<<endl;
        cin>>passwod;
        a->passwordArray[a->p_size].passcode=passwod;
        cout<<endl;

        cout<<"success!"<<endl;

        system("pause");
        system("cls");
    }

}


void showlist(struct passwordbox a)
{
    if(a.p_size==0)
    {
        cout<<"the box is empty."<<endl;
    }
    else
    {
        for (int i=1;i<a.p_size+1;i++)
        {
            cout<<"plateform:   "<<a.passwordArray[i].platform<<endl;
            cout<<"account:   "<<a.passwordArray[i].accout<<endl;
            cout<<"password:   "<<a.passwordArray[i].passcode<<endl;

            cout<<endl<<endl<<endl;

        }
    }
    
    system("pause");
    system("cls");
}

int isexist(struct passwordbox a,string platf)
{
    cout<<"please enter a platform:  ";
    cin>>platf;
    cout<<endl<<endl;
    for (int i=1;i<a.p_size+1;i++)
    {
        if(platf==a.passwordArray[i].platform)
        {
            return i;
        }
    }

    return -1;
}

void checkplatf(struct passwordbox a,int b)
{
    if (b==-1)
    {
        cout<<"you don't have such a platform in your password box."<<endl;
    }
    else
    {
        cout<<"you  have such a platform in your password box."<<endl<<endl;
        cout<<"plateform:   "<<a.passwordArray[b].platform<<endl;
        cout<<"account:   "<<a.passwordArray[b].accout<<endl;
        cout<<"password:   "<<a.passwordArray[b].passcode<<endl;

    }

}

void changecode(struct passwordbox * a,int b)
{
    string accot;
    cout<<"account:   "<<endl;
    cin>>accot;
    a->passwordArray[b].accout=accot;
    cout<<endl;

    string passwod;
    cout<<"password:   "<<endl;
    cin>>passwod;
    a->passwordArray[b].passcode=passwod;
    cout<<endl;

    cout<<" changing success!"<<endl;
    system("pause");
    system("cls");   
}

void delet_(struct passwordbox * a,int b)
{
    for(int i=b;i<a->p_size;i++)
    {
        a->passwordArray[i]=a->passwordArray[i+1];
    }
    a->p_size--;
    cout<<"the deletion is done!";
    system("pause");
    system("cls");   
}



int main () 
{   
    int rank;
    string platf;
    passwordbox box1;
    int select =0;
    box1.p_size=0;

    while (true)
    {
        showmenu();
        cout<<"now we have  "<<box1.p_size<<" groups of passwords."<<endl;
        cout<<endl;
        cout<<"enter your choice  ";
        cin>>select;
        cout<<endl;
    

        switch (select)
        {
        case 1:                       //1，添加密码
            addpassword(&box1);
            break;
    
        case 2:                       //2，显示所有密码
            showlist(box1);
            break;
        
        case 3:                       //3，删除密码
            {
                rank=isexist(box1,platf);
                checkplatf(box1,rank);
                cout<<endl<<endl;
                delet_(&box1,rank);
                break;
            }
        
        case 4:                       //4，查找密码
            rank=isexist(box1,platf);
            checkplatf(box1,rank);
            system("pause");
            system("cls");
            break;
        
        case 5:                       //5，修改密码
            rank=isexist(box1,platf);
            if(rank==-1)
            {
                cout<<"you don't have such a platform in your password box."<<endl;
                system("pause");
                system("cls");
            }
            else
            {
                changecode(&box1,rank);
            }
            
            break;
        
        case 6:                       //6，清除密码
        {  
            int answer;
            cout<<"Do you really want to delete this box?"<<endl;
            cout<<"1,yes i'm sure"<<endl;
            cout<<"2,no"<<endl;
            cin>>answer;
            if(answer==1)
            {
                box1.p_size=0;
                cout<<"the box has been emptified."<<endl;
                system("pause");
                system("cls");
            }
            else if(answer==2)
            {
                system("pause");
                system("cls");
            }
           
            break;
        }
        
        case 0:                       //0，退出密码箱  
        
            cout<<"thanks for your using"<<endl<<endl;
            cout<<"welcome for your next visiting"<<endl<<endl;
            system("pause");
            return 0;
            break;
    
    
        default:
            break;
        }

    }

}